//
//  rooteViewController.swift
//  上海微博
//
//  Created by zhangliming on 16/2/23.
//  Copyright © 2016年 zhangliming. All rights reserved.
//

import UIKit

//MARK:-定义所有公共属性

class rooteViewController: UIViewController {

    lazy var tableView = UITableView(frame: CGRectZero, style: .Plain)

    ///访客视图

    var vistorView :VisiterView?

    ///下拉刷新控件

    var refreshContrl: UIRefreshControl?

    ///上拉刷新控件

    var pullUpControl:UIActivityIndicatorView?

    ///刷新数据索引

    var dataIndex = 100

    var endIndex = 0

    ///刷新状态

    var pullUping = true

    ///是否登录

    var isLogin = false

     //数据数组

    lazy var dataList = [String]()

    override func loadView() {

        isLogin ? setupTabelView() : setUpVistorView()

    }

    override func viewDidLoad() {

        super.viewDidLoad()

        ///加载数据

        loadData()

    }

}

//MARK:-界面的搭建

extension rooteViewController {

    private func setupTabelView(){

        view = tableView

        tableView.dataSource = self

        tableView.delegate = self

    }

    ///设置访客视图

    private func setUpVistorView (){

        vistorView = VisiterView()

        view = vistorView

        ///设置导航栏的按钮

        navigationItem.rightBarButtonItem = UIBarButtonItem(
            title: "登录",

            style: .Plain,

            target: self,

            action: "didClickLogin")

        navigationItem.leftBarButtonItem = UIBarButtonItem(
            title: "注册",

            style: .Plain,

            target: self,

            action: "didRigester")

        ///添加登录按钮监听方法

        vistorView?.loginButton.addTarget(self, action: "didClickLogin", forControlEvents: .TouchUpInside)

        ///添加注册按钮监听方法
            
        vistorView?.rigisterButton.addTarget(self, action: "didRigester", forControlEvents: .TouchUpInside)
        

    }

    //MARK:-实例下拉刷新控件

    func prepareRefreshControl(){

        refreshContrl = UIRefreshControl()

        refreshContrl?.addTarget(self, action: "loadData", forControlEvents: .ValueChanged)

        tableView.addSubview(refreshContrl!)

        ///添加上拉刷新界面

        pullUpContrl()


    }

    //MARK:-实例化上拉刷新控件

    func pullUpContrl (){

    pullUpControl = UIActivityIndicatorView(activityIndicatorStyle: .Gray)

    pullUpControl?.color = UIColor.greenColor()

    tableView.tableFooterView = pullUpControl

    }
}

//MARK:-数据的加载

extension rooteViewController:UITableViewDataSource, UITableViewDelegate {

    func numberOfSectionsInTableView(tableView: UITableView) -> Int {

        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


        return 0
    }

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        return UITableViewCell()

    }

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {

        let sectionIndex = indexPath.section

        let row = tableView.numberOfRowsInSection(sectionIndex)-1

        print("\(indexPath.section)-\(indexPath.row)--\(row)")

        if sectionIndex == 0 && indexPath.row == row{

            print("上拉刷新")

            pullUpControl?.startAnimating()

            pullUping = true

            loadData()
            
        }

    }

//MARK:-登录 方法

    func didClickLogin (){

        ///跳转授权页面

        let loginContrller = UINavigationController(rootViewController: LoginViewController())

        self.presentViewController(loginContrller, animated: true, completion: nil)

    }

//MARK:-注册方法

    func didRigester (){

        print(__FUNCTION__)

    }

//MARK:-加载数据方法
    func loadData(){

        print("加载数据")

    }

//MARK:-刷新数据方法

    func refreshData(){

        var dataArray = [String]()

        if pullUping == true {

            for i in (0..<10) {

                dataArray.append("\(dataIndex - i)")

            }

            dataIndex = Int ( dataArray.last! )! - 1

            dataList += dataArray

            endIndex = Int ( dataList.first!)! + 1

        }else{

            for i in (0..<10).reverse() {

                dataArray.append("\(endIndex + i)")

            }

             dataList = dataArray + dataList
            
            endIndex = Int ( dataArray.first! )! + 1
            
        }
    }


    func endRefresh (){

        ///结束刷新控件动画

        refreshContrl?.endRefreshing()

        pullUpControl?.stopAnimating()

        pullUping = false

        ///刷新页面数据
        
        tableView.reloadData()
        
    }
}



